package com.neusoft.techan.controller;

import org.springframework.web.bind.annotation.*;

import com.neusoft.techan.model.Admin;
import com.neusoft.techan.service.AdminService;
import com.neusoft.techan.utils.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  


@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    
    /**
     * 登录
     */
    @ResponseBody
    @RequestMapping(value="/tologin",method=RequestMethod.POST)
    public Result<Admin> login(Admin admin,HttpServletRequest request){
    	Admin ad=adminService.login(admin);
    	if(ad!=null){
    		 HttpSession session = request.getSession();
    		 session.setAttribute("admin", ad);
    		 return ResultGenerator.genSuccessResult(ad);
    	}else{
    		 Result<Admin> result = new Result<Admin>();
    	     result.setResultCode(Constants.RESULT_CODE_SERVER_ERROR);
    	     return result;
    	}
    }
    
    /**
     * 登出
     */
    @ResponseBody
    @RequestMapping(value="/logout",method=RequestMethod.POST)
    public Result<?> logout(Admin admin,HttpServletResponse response,HttpServletRequest request){
    	//清除session
    	HttpSession session = request.getSession();
    	session.removeAttribute("admin");
    	return null;
    }
    
}